package com.lisn.activityresultsapi

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.lisn.activityresultsapi.R

class FirstActivity : AppCompatActivity() {
    private val TAG = "FirstActivity"
    private val requestDataLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == RESULT_OK) {
            val data = result.data?.getStringExtra("data")

            Log.e(TAG, "data: $data")
            // Handle data from com.lisn.activityresultsapi.SecondActivity
        }
    }

    // Create ActivityResultLauncher
//    private val launcher =
//            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
//                activityResult ->
//                // activityResult will return this as ActivityResult
//                Log.e("MainActivity", activityResult.toString())
//                //  D/MainActivity: ActivityResult{resultCode=RESULT_OK, data=Intent { (has extras) }}
//            }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)
        val firstButton = findViewById<Button>(R.id.first_button)
        firstButton.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            requestDataLauncher.launch(intent)
        }
    }

}